package reflect;

import org.junit.Test;

import java.lang.reflect.Field;

public class ReflectTest2 {

    Class<Person> p = Person.class;

    Person person = p.newInstance();

    public ReflectTest2() throws InstantiationException, IllegalAccessException {

    }

    @Test
    public void test1() throws NoSuchFieldException, IllegalAccessException {

        // 获取id属性,前提是public
        final Field id = p.getField("id");

        // 非静态的属性，依托对象
        // 设置那个对象的属性值
        id.set(person,123);

        // 获取那个对象的属性值
        final Object o = id.get(person);
        System.out.println(o);


    }


    @Test
    public void test2() throws NoSuchFieldException, IllegalAccessException {

        // 获取id属性,常用的方法
        final Field id = p.getDeclaredField("name");
        // 开启权限
        id.setAccessible(true);

        id.set(person,"WENJIAN");

        final Object o = id.get(person);

        System.out.println(o);




    }
}
